package com.runmi.webchat.aftersale.web.iframe.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.runmi.webchat.aftersale.common.page.PageModel;
import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.utils.StringUtil;
import com.runmi.webchat.aftersale.web.base.BaseController;
import com.runmi.webchat.aftersale.web.iframe.service.LoginService;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.runmi.webchat.aftersale.util.Constants;

@Controller
public class LoginController extends BaseController{

	
	@Autowired
	private LoginService loginService;
	
	
	@ApiOperation(value = "登陆", notes = "登陆", tags={ "Login", })
	@RequestMapping(value = "/login")
	public ModelAndView login(
			HttpServletRequest request,HttpServletResponse response,
			@ApiParam(value = "登陆用户名") @RequestParam(value = "username", required = false) String username,
			@ApiParam(value = "登陆用户名密码") @RequestParam(value = "password", required = false)  String password
			) throws ServletException, IOException{
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		ModelAndView mv =null;

		Map<String,Object> result= loginService.login(username, password);
		if(result.get(Constants.RETURN_CODE_KEY).toString().equals(Constants.RETURN_CODE_SUCCESS)){				
			writeCokie(request, response,Constants.SYSTEM_COOKIE,result.get("tocken").toString(),1);	
			mv= new ModelAndView("redirect:/");
			return mv;
		}else{
			request.setAttribute("error", result.get(Constants.RETURN_MESSAGE_KEY));
			//return "iframe/Login/login";
			mv= new ModelAndView("/iframe/Login/login");
			return mv;
		}		
		
//		Result result= loginService.login(username, password);
//		if(result.isSuccess()){				
//			writeCokie(request, response,"scm_tocken",result.getData().toString(),1);		
//			return "redirect:/";
//		}else{
//			request.setAttribute("error", result.getReturnMessage());
//			return "iframe/Login/login";
//		}		
	}
	
//	@ApiOperation(value = "登陆", notes = "账号密码登陆，登陆成功以后返回accessTocken", tags={ "框架", })
//	@RequestMapping(value = "/login",method = RequestMethod.POST)
//	public Result login(
//			HttpServletRequest request,HttpServletResponse response,
//			@ApiParam(value = "登陆用户名") @RequestParam(value = "loginName") String loginName,
//			@ApiParam(value = "登陆用户名密码") @RequestParam(value = "password")  String password
//			) throws ServletException, IOException{
//		request.setAttribute("loginName", loginName);
//		request.setAttribute("password", password);		
//		return loginService.login(loginName, password);
//		
//	}
//	@RequestMapping(value = "/")
//	public String index() {
//		//System.out.println("swagger-ui.html");
//		return "redirect:login";
//	}
	
	@ApiOperation(value = "登出", notes = "登出", tags={ "框架", })
	@RequestMapping(value = "/logout",method = RequestMethod.GET)
	public String logout(
			HttpServletRequest request,HttpServletResponse response
			) throws ServletException, IOException{		
			String tocken=this.readCokie(request, response, "scm_tocken");	
			if(StringUtil.isNotEmptyStr(tocken)){
				clearCokie(request, response,"scm_tocken");
			}
			return "iframe/Login/logout";
	}
}
